package com.kanche.bigbaby.collection.domain;

import android.content.ContentValues;

import com.google.gson.Gson;
import com.kanche.bigbaby.collection.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.kkche.bss.domain.Photo;
//import com.kkche.bss.utils.StringUtils;

//import com.kkche.merchant.utils.StringUtil;

/**
 * Created by nealmi on 6/16/14.
 */
public class Gallery implements Serializable {
    private String id;
    private transient Map<String, Photo> photoMap = new HashMap<String, Photo>();
    private List<Photo> photos;

    public static Gallery fromJson(String json) {
        Gson gson = new Gson();
        Gallery gallery = gson.fromJson(json, Gallery.class);
        if (gallery == null)
            gallery = new Gallery();
        gallery.populateMapping();
        return gallery;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Photo> getPhotos() {
        return photos;
    }

    public void setPhotos(List<Photo> photos) {
        this.photos = photos;
        populateMapping();
    }

    public Photo getPhoto(String key) {
        return photoMap.get(key);
    }

    public String getPhotoPath(String key) {
        Photo photo = photoMap.get(key);
        if (photo != null) {
            return photo.getUri();
        }
        return null;
    }

    public ContentValues toContentValues() {
        Gson gson = new Gson();
        ContentValues values = new ContentValues();
        values.put("gallery", gson.toJson(this));
        return values;
    }

    private void populateMapping() {
        if (photos != null) {
            for (Photo photo : photos) {
                photoMap.put(photo.getKey(), photo);
            }
        }
    }

    public boolean hasValidPhoto(String key) {
        return StringUtils.hasText(getPhotoPath(key));
    }

    public boolean hasValidPhoto() {
        return StringUtils.hasText(getFirstAvailablePhotoPath());
    }

    public String getFirstAvailablePhotoPath() {
        if (photos != null) {
            for (Photo photo : photos) {
                if (StringUtils.hasText(photo.getUri())) {
                    return photo.getUri();
                }
            }
        }
        return null;
    }

    public List<Photo> getAvailableLocalPhotos() {
        List<Photo> availablePhotos = new ArrayList<Photo>();
        if (photos != null) {
            for (Photo photo : photos) {
                if (StringUtils.hasText(photo.getUri()) && !photo.getUri().startsWith("http")) {
                    availablePhotos.add(photo);
                }
            }
        }
        return availablePhotos;
    }

    public List<Photo> getAllAvailablePhotos() {
        List<Photo> availablePhotos = new ArrayList<Photo>();
        if (photos != null) {
            for (Photo photo : photos) {
                if (StringUtils.hasText(photo.getUri())) {
                    availablePhotos.add(photo);
                }
            }
        }
        return availablePhotos;
    }


    public void mergePhotos(List<Photo> uploadedPhotos) {

        for (Photo photo : photos) {

        }
    }

    public void update(Photo target) {
        for (Photo photo : photos) {
            if (photo.getKey().equals(target.getKey())) {
                photo.setUri(target.getUri());
                break;
            }
        }
    }
}
